using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace Lync.Widget.Library.Configuration
{
    public class ActiveDirectoryMappingConfiguration : IActiveDirectoryMappingConfiguration
    {
        private readonly ActivedirectoryMappingElementCollection m_Mappings;

        private ActiveDirectoryMappingConfiguration(ActivedirectoryMappingElementCollection mappings)
        {
            m_Mappings = mappings;
        }
        
        public IList<IActiveDirectoryMap> Mappings
        {
            get { return m_Mappings.Cast<IActiveDirectoryMap>().ToList(); }
        }

        public static ActiveDirectoryMappingConfiguration Load()
        {
            var section = ConfigurationManager.GetSection("activedirectoryMappings") as ActivedirectoryMappingConfigurationSection;

            if (section == null)
            {
                throw new InvalidOperationException("activedirectoryMappings section not found");
            }

            return new ActiveDirectoryMappingConfiguration(section.ActivedirectoryMapping);
        }

    }
}